package com.oocl.myHomework.controller;

import java.util.Scanner;

import com.oocl.myHomework.command.Command;
import com.oocl.myHomework.command.CommandUtil;
import com.oocl.myHomework.entity.Person;
import com.oocl.myHomework.service.CRMService;
import com.oocl.myHomework.service.CRMServiceImpl;
import com.oocl.myHomework.util.SortUtil;
import com.oocl.myHomework.view.View;

public class Controller {
	
	CRMService crmService ;
	
	View view ;
	 
	public void initial(){
		
		view = new View();
		
		crmService = new CRMServiceImpl();
		
		crmService.initData();
	}
	
    
    
    public  void operationChoice(String commandstr) throws Exception{
    	
    	
    	Command command = CommandUtil.resolveCommand(commandstr);
    	if(command.getKey().equals("")||command.getKey()==null){
    		
    	}else if(!CommandUtil.contain(command.getKey())){
    		System.out.println("It is not exist the command '"+commandstr+"' ");
    	}else{
		  switch(command.getOprationType()){
		  case "L":{
			  view.showCurCommand(commandstr);
			  LookUpAll();
			break;
		  }
		  case "L1":{
			    view.showCurCommand(commandstr);
				LookUpByCondition(command.getValue());
				break;
			  }
		  case "A1":{
			    view.showCurCommand(commandstr);
				add(command.getValue());
				break;
			  }
		 case "D1":{
			 view.showCurCommand(commandstr);
			delete(command.getValue());
			break;
		 }
		case "U2":{
			view.showCurCommand(commandstr);
			update(command.getValue());  
			break;
		}
		case "O": {
			view.showCurCommand(commandstr);
			orderById();
			break;
		}case "O1": {
			view.showCurCommand(commandstr);
			orderByName();
			break;
		}
		case "H": {
			view.showCurCommand(commandstr);
			view.helpView();
			break;
		}
		case "Q": {
			System.out.println("Quit");
			return ;
		}
		
		default:
			break;
		}
    	}
    }
    
    
    private void LookUpByCondition(String condition) throws Exception {
		// TODO Auto-generated method stub
    	String[] strs = condition.split(":");
    	Person[] ps = crmService.find();
    	Person[] persons =  new  Person[ps.length];
    	if(strs[0].trim().equals("id")){
    		int idValue = Integer.parseInt(strs[1].trim());
    		int target = findTarget(ps,idValue);
    		persons = new Person[]{ps[target]};
    	}else{
    		persons = crmService.findByName(strs[1]);
    	}
    	view.printList(persons);
	}


    private void LookUpAll() throws Exception{
    	Person[] persons = crmService.find();
    	view.printList(persons);
    }
  
    
    
    
    public void add(String value) throws Exception{
    	Person person = new Person();
    	String[] strs = value.split(":|,");
    	for(int i=0 ;i<strs.length;i++){
    		if(i%2==0){
    			if(strs[i].trim().equals("name")){
    				person.setName(strs[i+1].trim());
    			}else if(strs[i].trim().equals("sex")){
    				person.setSex(strs[i+1].trim());
    			}else if(strs[i].trim().equals("birthday")){
    				person.setBirthday(strs[i+1].trim());
    			}else if(strs[i].trim().equals("address")){
    				person.setAddress(strs[i+1].trim());
    			}else if(strs[i].trim().equals("call")){
    				person.setCall(strs[i+1].trim());
    			}
    			
    		}
    	}
    	int curMaxId = crmService.getMaxId();
    	person.setId(curMaxId+1);
        crmService.addPerson(person);
        System.out.println("save succefull!");
    	
    } 
    
    public void delete(String idStr) throws Exception{
    	   Person[] persons = crmService.find();
		   int id = Integer.parseInt(idStr);
		   int target = findTarget(persons,id);
		   if(target==-1){
			   System.out.println("It is not exist id '"+idStr+"'");
		   }else{
			   crmService.delete(persons[target]);
			   System.out.println("delete succefull!");
		   }
	    			
    } 
    
   private int findTarget(Person[] persons, int id) {
		// TODO Auto-generated method stub
	   int target = -1;
	   for(int i=0;i<persons.length;i++){
		   if(persons[i].getId()==id){
			   target = i;
			   break;
		   }
	   }
		return target;
	}


public void update(String value) throws Exception{
	   Person[] persons = crmService.find();
	   String[] sts =value.split(" ");
	   String idStr = sts[0];
	   String condition = sts[1];
	   int id = Integer.parseInt(idStr);
	   Person p = null;
	   for(Person person:persons){
		   if(person.getId()==id){
			p = person;
	        break;
		   }
	   }
	   if(p==null){
		   System.out.println("It is not  exist id '"+id+"'");
	   }else{
   			String[] strs = condition.split(",|:");
   		   for(int i =0 ;i<strs.length;i++){
   			   if(i%2==0){
   				   if(strs[i].trim().equals("name")){
   					   p.setName(strs[i+1].trim());
   				   }else if(strs[i].trim().equals("sex")){
   					   p.setSex(strs[i+1].trim());
   				   }else if(strs[i].trim().equals("birthday")){
   					   p.setBirthday(strs[i+1].trim());
   				   }else if(strs[i].trim().equals("address")){
   					   p.setAddress(strs[i+1].trim());
   				   }else if(strs[i].trim().equals("call")){
   					   p.setCall(strs[i+1].trim()); 
   				   }  
   			   }
   		   }
   		   crmService.update(p);
   			System.out.println("update succefull!");
	   }
    } 
   public void orderById() throws Exception{
	   Person[] persons = crmService.find();
	   SortUtil.sortById(persons);
	   view.printList(persons);
	   
   }
   
   
   public void orderByName() throws Exception{
	   Person[] persons = crmService.find();
	   SortUtil.sortByName(persons);
	   view.printList(persons);
	   
   }
   
   
}
